/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.mahasen.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.File;

import org.mahasen.util.DeleteUtil;
import org.mahasen.MahasenConstants;


public class DeleteRequestServlet extends HttpServlet {

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request, response);

    }

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String fileName = request.getParameter(MahasenConstants.FILE_NAME);
        DeleteUtil deleteUtil = new DeleteUtil();

        System.out.println("File to Delete :" + fileName);

        try {

            String repository = deleteUtil.getRepositoryPath();
            File fileToDelete = new File(repository + fileName);
            // Make sure the file or directory exists and isn't write protected
            if (!fileToDelete.exists())
                throw new IllegalArgumentException(
                        "Delete: no such file or directory: " + fileName);

            if (!fileToDelete.canWrite())
                throw new IllegalArgumentException("Delete: write protected: "
                        + fileName);

            // If it is a directory, make sure it is empty
            if (fileToDelete.isDirectory()) {
                String[] files = fileToDelete.list();
                if (files.length > 0)
                    throw new IllegalArgumentException(
                            "Delete: directory not empty: " + fileName);
            }

            // Attempt to delete it
            boolean success = fileToDelete.delete();

            if (!success)
                throw new IllegalArgumentException("Delete: deletion failed");
        } catch (Exception e) {
            e.printStackTrace();

        }


    }
}
